package net.zjitc.controller.admin;

import net.zjitc.mapper.OrderItemMapper;
import net.zjitc.pojo.Order;
import net.zjitc.pojo.OrderItem;
import net.zjitc.pojo.Products;
import net.zjitc.pojo.User;
import net.zjitc.service.OrderItemService;
import net.zjitc.service.OrderService;
import net.zjitc.service.ProductService;
import net.zjitc.service.UserService;
import net.zjitc.vo.OrderConditionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Author admin
 * @Date 2019/4/9 8:12
 */
@Controller
@RequestMapping("/admin/order")
public class OrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    UserService userService;
    @Autowired
    OrderItemService orderItemService;
    @Autowired
    ProductService productService;
    @RequestMapping(value = "/list")
    public String All_list(Model model){
        List<Order>orderList = orderService.findAll();
        for (Order o:orderList
             ) {
            User user = userService.findById(o.getUser_id());
            o.setUser(user);
        }
        model.addAttribute("orders",orderList);
        return "/admin/orders/list.jsp";
    }

    @RequestMapping(value = "/findOrderById")
    public String findOrderById(String id,Model model){
        Order order = orderService.findById(id);
        User user = userService.findById(order.getUser_id());
        order.setUser(user);
        List<OrderItem> itemList = orderItemService.findByOId(order.getId());
        for (OrderItem item:itemList){
            Products product = productService.findById(item.getPid());
            item.setProducts(product);
        }
        order.setOrderItemList(itemList);
        model.addAttribute("order",order);
        return "/admin/orders/view.jsp";
    }

    @RequestMapping("/findByCondition")
    @ResponseBody
    public List<Order> findByCondition(OrderConditionVo vo){
        List<Order> list = orderService.findByCondition(vo);
        return list;
    }

    @RequestMapping(value = "/delOrderById")
    public String delOrderById(String id){
        orderService.delete(id);
        return "redirect:/admin/order/list";
    }
}
